﻿package Gameplay
{	
	import Gameplay.Events.MarkFailedEvent;

	import flash.events.Event;
	import flash.media.SoundChannel;	

	public class FakeTrack extends AbstractTrack
	{
		public const BOT_HIT:String = 'BOT_HIT';
		
		private var FInfo:BonusInfo;
		private var FBotTrack:Boolean;
		protected var FAllGood:Boolean;
		protected var FGoodPoints:int;
		protected var FPlayerScore:int;		
		
		public function FakeTrack() 
		{
			super();
			FBotTrack = false;
			FInfo = new BonusInfo();
			FAllGood = false;
			FGoodPoints = 0;
			FPlayerScore = 0;
		}
		
		public function Play():void
		{
			FPlayerScore = 0;
			addEventListener(Event.ENTER_FRAME, OnEnterFrame);
		}
		
		public function set Channel(AChannel:SoundChannel):void
		{
			FChannel = AChannel;
		}
		
		private function OnEnterFrame(e:Event):void
		{
			FContainer.y = FChannel.position / FSound.length * FContainer.height - FContainer.height + FContainer.TrashHold;
		}
		
		public function ExternalBit(AID:int, APoints:int, ABonusType:int, ABonusPoints:int):void
		{
			FInfo.ID = AID;
			FInfo.BonusType = ABonusType;
			FInfo.Points = APoints;
			FInfo.BonusPoints = ABonusPoints;
			PerformKeyPress(FInfo.ID);
		}
		
		override protected function PerformKeyPress(ATrackID:int):BonusInfo
		{
			trace(FInfo.BonusType);
			if(FInfo.BonusType != FInfo.BAD)
			{
				FStrips[ATrackID].Show(true);
				FBadText.visible = false;
				switch(FInfo.BonusType)
				{
					case FInfo.COMBO:
					{
						FGoodText.text = 'КОМБО ' + FInfo.BonusPoints;
						FStatistic.Combo++;
						break;
					}					
					case FInfo.GOOD:
					{
						FGoodText.text = 'ХОРОШО ' + FInfo.BonusPoints;
						FStatistic.Good++;
						break;
					}					
					case FInfo.PERFECT:
					{
						FGoodText.text = 'ОТЛИЧНО ' + FInfo.BonusPoints;
						FStatistic.Perfect++;
						break;
					}
				}
			}
			else
			{
				FStrips[ATrackID].Show(false);				
				FGoodText.text = '';
				FBadText.visible = true;
				FStatistic.Bad++;
			}			
			FStatistic.Score = FInfo.Points;
			
			return FInfo;
		}
		
		public function set Bot(ABot:Boolean):void
		{
			if(ABot)
			{
				addEventListener(MarkFailedEvent.MARK_FAILED, OnMarkFail);
			}
			else
			{
				removeEventListener(MarkFailedEvent.MARK_FAILED, OnMarkFail);
			}
			FBotTrack = ABot;	
		}
		
		private function OnMarkFail(e:MarkFailedEvent):void
		{
			var vLuck:Boolean;
			if((FPlayerScore - Statistic.Score) > 0)
				vLuck = true;
			else
				vLuck = Boolean(Math.round(Math.random() * 10) > 4);			
			
			BotPress(vLuck, e.StripID);
		}
		
		private function BotPress(ALucky:Boolean, ATrackID:int)
		{
			var vResult:BonusInfo = new BonusInfo();			
			vResult.Points = 0;
			vResult.ID = ATrackID;			
			if(ALucky)
			{
				vResult.Points++;
				FStrips[ATrackID].Show(true);
				FAllGood = true;
				FGoodPoints++;
			}
			else
			{
				FStrips[ATrackID].Show(false);
				FAllGood = false;
				FGoodPoints = 0;
				FGoodText.text = '';			
				FBadText.visible = true;
				FStatistic.Bad++;
			}
			
			if(FAllGood)
			{
				FBadText.visible = false;
				var vBonusPoints:int = 0;
				if(FGoodPoints > 1 && FGoodPoints < 6)
				{
					FGoodText.text = 'КОМБО ' + FGoodPoints;					
					FStatistic.Combo++;
				}
				else if(FGoodPoints == 6)
				{					
					vResult.Points += 100;
					vBonusPoints = FGoodPoints - 5;
					FGoodText.text = 'ХОРОШО ' + vBonusPoints;					
					vResult.Points += 2 * vBonusPoints;
					FStatistic.Good++;
				}
				else if(FGoodPoints > 6)
				{
					vBonusPoints = FGoodPoints - 5;
					FGoodText.text = 'ХОРОШО ' + vBonusPoints;					
					vResult.Points += 2 * vBonusPoints;
					FStatistic.Good++;
					if(FGoodPoints == 11)
					{
						vBonusPoints = FGoodPoints - 10;
						FGoodText.text = 'ОТЛИЧНО ' + vBonusPoints;
						vResult.Points += 3 * vBonusPoints;
						FStatistic.Perfect++;
					}
					else if(FGoodPoints > 11)
					{
						vBonusPoints = FGoodPoints - 10;
						FGoodText.text = 'ОТЛИЧНО ' + vBonusPoints;
						vResult.Points += 3 * vBonusPoints;
						FStatistic.Perfect++;
					}
				}
			}			
			FStatistic.Score += vResult.Points;			
			dispatchEvent(new Event(BOT_HIT));
		}
		
		public function set PlayerScore(AScore:int):void
		{
			FPlayerScore = AScore;
		}
	}	
}
